package com.atguigu.service.Impl;

import com.atguigu.mapper.RefundInfoMapper;
import com.atguigu.service.RefundInfoService;
import com.atguigu.yygh.enums.RefundStatusEnum;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.model.order.RefundInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
//退款
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {


    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //判断退款记录是否存在
        QueryWrapper<RefundInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("order_id",paymentInfo.getOrderId());
        queryWrapper.eq("payment_type",paymentInfo.getPaymentType());
        RefundInfo refundInfo = baseMapper.selectOne(queryWrapper);
        if(refundInfo!=null){
            return  refundInfo;
        }
        //如果退款记录为空的话 就要创建一个退款记录
        RefundInfo  refundInfo1 =new RefundInfo();
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus()); //1 退款中
        refundInfo.setCreateTime(new Date());

       baseMapper.insert(refundInfo);

        return refundInfo;
    }
}
